home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / tools.c < prev    next >
C/C++ Source or Header  |  1994-11-24  |  2KB  |  68 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.00 -- module tools.c                     */
  5. /*                                         */
  6. /*    Miscellaneous utility functions.                     */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifndef _POSIX_SOURCE
  14. #define _POSIX_SOURCE
  15. #endif
  16. #include "xsok.h"
  17.  
  18. void fatal(const char *msg, ...) {
  19.     va_list args;
  20.  
  21.     va_start(args, msg);
  22.     vfprintf(stderr, msg, args);
  23.     fprintf (stderr, "\n");
  24.     exit (1);
  25. }
  26.  
  27. void *malloc_(size_t n) {
  28.     void *p;
  29.     if (!n)
  30.     return NULL;    /* since malloc(0) may return NULL */
  31.     p = malloc(n);
  32.     if (!p)
  33.     fatal("out of memory");
  34.     return p;
  35. }
  36.  
  37. void *calloc_(size_t n, size_t s) {
  38.     void *p;
  39.     if (!n)
  40.     return NULL;    /* WATCOM C says "out of memory" in the case n = 0 */
  41.     if (!(p = calloc(n, s)))
  42.         fatal("out of memory");
  43.     return p;
  44. }
  45.  
  46. void *realloc_(void *p, size_t n) {
  47.     if (p == NULL)    /* no old block of size > 0 exists */
  48.     return malloc_(n);
  49.     if (!n) {
  50.     free_(p);
  51.     return NULL;
  52.     }
  53.     if (!(p = realloc(p, n)))
  54.         fatal("out of memory\n");
  55.     return p;
  56. }
  57.  
  58. void free_(void *p) {
  59.     if (p)
  60.     free(p);
  61. }
  62.  
  63. char *strsav(const char *txt) {
  64.     char *p = malloc_(1 + strlen(txt));
  65.     strcpy(p, txt);
  66.     return p;
  67. }
  68.